package com.lanyun.interceptor;/**
 * @author zhy
 * @date 2021/1/12 - 17:26
 */

import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**

 @ClassName LoginAspect
 @Description xxx
 @Author zhy
 @Date 2021/1/12 - 17:26
 @Version 1.0
 **/
@Aspect
@Component
public class LoginAspect {

//    @Before("execution(* com.lanyun.controller..*.*(..))")
    public void loginIntercept() throws IOException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
        HttpSession session = request.getSession();
        System.out.println(session.getAttribute("loginer"));
        if(session.getAttribute("loginer")==null||session.getAttribute("loginer")==""){
            System.out.println("管理员没有登陆");
            response.sendRedirect(request.getContextPath() + "/login");
        } else {
            System.out.println("管理员已经登陆");
        }

    }
}
